package cn.lanqiao.thread;

public class TestThread {
    public static int i = 100;
    public static final Object lock = new Object();

    public static void main(String[] args) { // main线程
        MyThread1 myThread1 = new MyThread1();
        myThread1.start();
//        MyThread1 myThread3 = new MyThread1();
//        myThread3.start();
        MyThread2 myThread2 = new MyThread2();
        new Thread(myThread2).start();
    }
}

class MyThread1 extends Thread {

    @Override
    public void run() {
        while (true) {
            synchronized (TestThread.lock) {
                if (TestThread.i > 0) {
                    System.out.println(Thread.currentThread().getName() + " -- " + TestThread.i);
                    TestThread.i--;
                } else {
                    break;
                }
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class MyThread2 implements Runnable {
    @Override
    public void run() {
        while (true) {
            synchronized (TestThread.lock) {
                if (TestThread.i > 0) {
                    System.out.println(Thread.currentThread().getName() + " -- " + TestThread.i--);
                } else {
                    break;
                }
            }
        }
    }
}
